package com.em.sugarfight.util;

import com.em.sugarfight.entity.*;
import com.em.sugarfight.entity.info.BombGridInfo;
import com.em.sugarfight.entity.info.MapItemGridInfo;
import com.em.sugarfight.entity.info.PlayerGridInfo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
/**
 * 游戏状态适配器 - 统一坐标系统的游戏状态访问
 */
public class GameStateAdapter {
    private GameState gameState;

    public GameStateAdapter(GameState gameState) {
        this.gameState = gameState;
    }

    /**
     * 获取我的玩家的格子坐标
     */
    public Position getMyPlayerPosition() {
        PixelPosition pixelPos = gameState.getMy_player().getPosition();
        return CoordinateConverter.pixelToGrid(pixelPos);
    }

    /**
     * 获取其他玩家的格子坐标
     */
    public List<PlayerGridInfo> getOtherPlayersPositions() {
        List<PlayerGridInfo> result = new ArrayList<>();

        for (PlayerStateVo player : gameState.getOther_players()) {
            PixelPosition pixelPos = player.getPosition();
            Position gridPos = CoordinateConverter.pixelToGrid(pixelPos);
            result.add(new PlayerGridInfo(player, gridPos));
        }

        return result;
    }

    /**
     * 获取所有炸弹的格子坐标（已经是格子坐标了）
     */
    public List<BombGridInfo> getBombsPositions() {
        List<BombGridInfo> result = new ArrayList<>();

        for (Bomb bomb : gameState.getBombs()) {
            // 炸弹的Position就是格子坐标
            Position gridPos = new Position(bomb.getPosition().getX(), bomb.getPosition().getY());
            result.add(new BombGridInfo(bomb, gridPos));
        }

        return result;
    }

    /**
     * 获取所有道具的格子坐标（已经是格子坐标了）
     */
    public List<MapItemGridInfo> getMapItemsPositions() {
        List<MapItemGridInfo> result = new ArrayList<>();

        for (MapItem item : gameState.getMap_items()) {
            // 道具的Position就是格子坐标
            Position gridPos = new Position(item.getPosition().getX(), item.getPosition().getY());
            result.add(new MapItemGridInfo(item, gridPos));
        }

        return result;
    }

    /**
     * 根据格子坐标获取地图格子信息
     * 注意：文档中提到map[y][x]的访问方式
     */
    public Cell getCell(Position gridPos) {
        Cell[][] map = gameState.getMap();
        // 根据文档：格子坐标为(27,15)的格子，在地图中获取格子信息的方式为map[15][27]
        // 所以访问方式是 map[gridY][gridX]
        return map[gridPos.getY()][gridPos.getX()];
    }

    /**
     * 检查格子位置是否可通行
     */
    public boolean isWalkable(Position gridPos) {
        if (!CoordinateConverter.isValidPosition(gridPos)) {
            return false;
        }

        Cell cell = getCell(gridPos);
        String terrain = cell.getTerrain().name();

        // 不可破坏障碍物不可通行
        return !"I".equals(terrain) && !"N".equals(terrain);
    }

    // 原始gameState的访问方法
    public GameState getOriginalGameState() {
        return gameState;
    }

    public int getCurrentTick() {
        return gameState.getCurrent_tick();
    }

    public String getMatchId() {
        return gameState.getCurrent_match_id();
    }
}
